package com.boe.csb.core.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.chinatricom.message.IDeliverMsg;
import com.chinatricom.message.IReportMsg;
import com.chinatricom.message.ISubmitMsg;
import com.chinatricom.slidewindow.SMSCallback;
import com.chinatricom.smsclient.ctcpp.CtcppChannel;
import com.chinatricom.smsclient.message.DeliverMsg;
import com.chinatricom.smsclient.message.ReportMsg;
import com.chinatricom.smsclient.message.SubmitMsg;

/**
 * 京东方短信发送接口 
 * FIXME 该方法废弃掉,因为京东方只提供ws的调用方式
 * Date 2016年12月12日
 * @author xc
 */
public class BoeMobileMsgSender implements IMobileMsgSender {

	private static final Logger logger = LoggerFactory.getLogger(BoeMobileMsgSender.class);

	public static CtcppChannel channel = null;
	
	private String host;
	
	private int port;
	
	private String user;
	
	private String pwd;
	
	private String regMsgTpl;
	
	private static BoeMobileMsgSender instance;
	
	private BoeMobileMsgSender(){}
	
	public static BoeMobileMsgSender getInstance(){
		if(instance == null){
			instance = new BoeMobileMsgSender();
			//int rst = instance.init();
			//System.out.println(rst);
		}
		
		return instance;
	}

	private int init() {
		if (channel != null && channel.isConnected()) {
			return 0;
		}
		
		host = PropertyUtils.getProperty("boe.sms.host");
		port = PropertyUtils.getIntProperty("boe.sms.port");
		user = PropertyUtils.getProperty("boe.sms.user");
		pwd = PropertyUtils.getProperty("boe.sms.pwd");
		regMsgTpl = PropertyUtils.getProperty("boe.sms.regmsg.tpl");

		CqBSMSCallback callback = new CqBSMSCallback();
		int wndSize = 16;
		int timeout = 60 * 1000;
		int trys = 3;
		int speed = 0;

		channel = new CtcppChannel(host, port, user, pwd, 1, wndSize, timeout,
				trys, speed, callback, 0, 0, 2, 1);
		return channel.start();
	}

	public int reset(String host, int port, String user, String pwd) {
		if (channel != null && channel.isConnected()) {
			channel.stop();
		}

		return init();
	}

	/**
	 * 发送注册短信验证码 FIXME 短信业务标识
	 * @param code
	 * @return
	 */
	@Override
	public boolean sendRegVerifyCode(String code, String recNum) {
		try {
			String context = getRegMsg(code);
			SubmitMsg msg = new SubmitMsg();
			msg.setTo(recNum);// 手机号
			msg.setMsg(context);// 短信内容
			msg.setMtTypeId(2);
			msg.setFrom("5975");
			boolean rst = channel.submit(msg);
			System.out.println(rst);
		} catch (Exception e) {
			logger.error("", e);
			return false;
		}

		return true;
	}
	
	private String getRegMsg(String code){
		return regMsgTpl.replace("${vcode}", code);
	}

}

class CqBSMSCallback implements SMSCallback {

	private static final Logger logger = LoggerFactory
			.getLogger(CqBSMSCallback.class);

	/**
	 * 这个函数接收上行短信，短信的各个字段都放在DeliverMsg
	 */
	@Override
	public boolean onDeliverSMS(IDeliverMsg arg0) {
		DeliverMsg msg = (DeliverMsg) arg0;
		if (msg != null) {
			logger.info("收到deliverMsg:phone={},内容={}, to={}", new Object[]{ msg.getFrom(), msg.getMsg(), msg.getTo()});
		}
		return true;
	}

	/**
	 * 这个函数接收下发短信的状态报告，它通过msgid关联到下行短信 result : 0———成功 1——等待发送 2——失败
	 */
	@Override
	public boolean onMTReportSMS(IReportMsg arg0) {
		ReportMsg msg = (ReportMsg) arg0;
		if (msg != null) {
			logger.info("收到repot: result:{}", msg.getResult());
		}
		return true;
	}

	/**
	 * 这个函数接收下行短信的response,SubmitMsg里面有了result和msgId,
	 * 这里的msg就是_chan.submit(_msg)里面的_msg. 
	 * result = 0 表示成功, 其他均为失败。
	 */
	@Override
	public boolean onSubmitedSMS(ISubmitMsg arg0) {
		SubmitMsg msg = (SubmitMsg) arg0;
		if (msg != null) {
			logger.info("收到submit response:result={}, msgid={}", msg.getResult(), msg.getMsgId());
		}
		return true;

	}

}
